//
//  YCRegisterImproveViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/7.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  完善个人资料

#import "YCRegisterImproveViewController.h"
#import <FXBlurView/FXBlurView.h>
#import "YCPickerContainerView.h"
#import "YCRegisterImprovePhotoPickerView.h"


@interface YCRegisterImproveViewController ()<YCPickerContainerViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

/** scrollView  */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 头像大背景  */
@property (nonatomic, strong) UIImageView *contentImageView;

///完善个人资料
@property (nonatomic,readonly) UILabel *improveInfoLabel;
///头像小背景
@property (nonatomic,readonly) UIImageView *iconBgImageView;
///头像
@property (nonatomic,readonly) UIImageView *iconImageView;

///昵称+星座
@property (nonatomic,readonly) UILabel *nickerAstroLabel;
///地址
@property (nonatomic,readonly) UILabel *placeLabel;


///出生年月
@property (nonatomic,readonly) UIButton *birthdyButton;
///现居于
@property (nonatomic,readonly) UIButton *placeButton;

///完成
@property (nonatomic,readonly) UIButton *completeButton;

/** datepicker */
@property (nonatomic, strong) UIDatePicker *datePicker;
/** citypicker */
@property (nonatomic, strong) UIPickerView *addressPickerView;
@property (nonatomic, strong) YCPickerContainerView *pickerContainerView;
/** citys */
@property (nonatomic, strong) NSArray *citys;


@property (nonatomic, strong) YCLoginUser *userModel;

@end

@implementation YCRegisterImproveViewController

@synthesize improveInfoLabel = _improveInfoLabel;
@synthesize nickerAstroLabel = _nickerAstroLabel;
@synthesize placeLabel = _placeLabel;
@synthesize iconBgImageView = _iconBgImageView;
@synthesize iconImageView = _iconImageView;
@synthesize birthdyButton = _birthdyButton;
@synthesize placeButton = _placeButton;
@synthesize completeButton = _completeButton;

#pragma mark - 懒加载
- (UIDatePicker *)datePicker
{
    if(!_datePicker)
    {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        _datePicker.maximumDate = [NSDate date];
        _datePicker.minimumDate = [@"1900-01-01" yc_dateWithDateFormatString:@"yyyy-MM-dd"];
        _datePicker.date = [@"1990-01-01" yc_dateWithDateFormatString:@"yyyy-MM-dd"];
        _datePicker.backgroundColor = [UIColor whiteColor];
    }
    return _datePicker;
}
- (UIPickerView *)addressPickerView
{
    if(!_addressPickerView)
    {
        _addressPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 200)];
        _addressPickerView.dataSource = self;
        _addressPickerView.delegate = self;
        _addressPickerView.backgroundColor = [UIColor whiteColor];
    }
    return _addressPickerView;
}

- (YCPickerContainerView *)pickerContainerView
{
    if(!_pickerContainerView)
    {
        _pickerContainerView = [[YCPickerContainerView alloc] init];
        _pickerContainerView.delegate = self;
    }
    return _pickerContainerView;
}
- (NSArray *)citys
{
    if(!_citys)
    {
//        _citys = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"]];
        _citys = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"allCS" ofType:@"plist"]];
    }
    return _citys;
}

// 完善个人资料
- (UILabel *)improveInfoLabel
{
    if(!_improveInfoLabel)
    {
        _improveInfoLabel = [[UILabel alloc] init];
        _improveInfoLabel.text = @"完善个人资料";
        _improveInfoLabel.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        _improveInfoLabel.textAlignment = NSTextAlignmentCenter;
        _improveInfoLabel.font = [UIFont systemFontOfSize:16];
    }
    return _improveInfoLabel;
}

// 昵称与星座
- (UILabel *)nickerAstroLabel
{
    if(!_nickerAstroLabel)
    {
        _nickerAstroLabel = [[UILabel alloc] init];
//        _nickerAstroLabel.text = @"Leelan 狮子座";
        _nickerAstroLabel.textColor = [UIColor whiteColor];
        _nickerAstroLabel.textAlignment = NSTextAlignmentCenter;
        _nickerAstroLabel.font = [UIFont systemFontOfSize:15];
        _nickerAstroLabel.hidden = YES;
    }
    return _nickerAstroLabel;
}

// 地址
- (UILabel *)placeLabel
{
    if(!_placeLabel)
    {
        _placeLabel = [[UILabel alloc] init];
//        _placeLabel.text = @"广东 广州";
        _placeLabel.textColor = [UIColor whiteColor];
        _placeLabel.textAlignment = NSTextAlignmentCenter;
        _placeLabel.font = [UIFont systemFontOfSize:13];
        _placeLabel.hidden = YES;
    }
    return _placeLabel;
}
// 头像
- (UIImageView *)iconImageView
{
    if(!_iconImageView)
    {
        _iconImageView = [[UIImageView alloc] init];
        UIImage *image  = [UIImage imageNamed:@"头像"];
        if (image.size.width < 200) {
//            image = [image yc_roundImageWithSize:CGSizeMake(200, 200) radius:100];
        }
        _iconImageView.image = image;
//        _iconImageView.image = [UIImage imageNamed:@"mf_bg_img"];
        __block CGFloat width = 0;
        [YCAdapter performOtherBlock:^{
            width = 165;
        } IPhone6Block:^{
            width = 195;
        } IPhone6pBlock:^{
            width = 205;
        }];
        _iconImageView.layer.cornerRadius = width / 2.0;
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.userInteractionEnabled = YES;
//        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        UITapGestureRecognizer *tapIcon = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIcon:)];
        [_iconImageView addGestureRecognizer:tapIcon];
    }
    return _iconImageView;
}

// 头像背景
- (UIImageView *)iconBgImageView
{
    if(!_iconBgImageView)
    {
        _iconBgImageView = [[UIImageView alloc] init];
        _iconBgImageView.image = [UIImage imageNamed:@"头像框"];
        _iconBgImageView.userInteractionEnabled = YES;
        __block CGFloat width = 0;
        [YCAdapter performOtherBlock:^{
            width = 190;
        } IPhone6Block:^{
            width = 232;
        } IPhone6pBlock:^{
            width = 240;
        }];
        _iconBgImageView.layer.cornerRadius = width / 2.0;
        _iconBgImageView.layer.masksToBounds = YES;
        _iconBgImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconBgImageView.clipsToBounds = YES;
    }
    return _iconBgImageView;
}

- (UIButton *)birthdyButton
{
    if(!_birthdyButton)
    {
        _birthdyButton = [[UIButton alloc] init];
        _birthdyButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [_birthdyButton setTitleColor:[UIColor yc_colorWithUInt:0x3d3e40] forState:UIControlStateNormal];
        _birthdyButton.backgroundColor = [UIColor whiteColor];
        _birthdyButton.layer.cornerRadius = 17.5;
        [_birthdyButton setTitle:NSLocalizedString(@"出生年月", nil) forState:UIControlStateNormal];
        [_birthdyButton addTarget:self action:@selector(didTouchUpInsideBirthdayButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _birthdyButton;
}


- (UIButton *)placeButton
{
    if(!_placeButton)
    {
        _placeButton = [[UIButton alloc] init];
        _placeButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [_placeButton setTitleColor:[UIColor yc_colorWithUInt:0x3d3e40] forState:UIControlStateNormal];
        _placeButton.backgroundColor = [UIColor whiteColor];
        _placeButton.layer.cornerRadius = 17.5;
        [_placeButton setTitle:NSLocalizedString(@"现居于", nil) forState:UIControlStateNormal];
        [_placeButton addTarget:self action:@selector(didTouchUpInsidePlaceButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _placeButton;
}

- (UIButton *)completeButton
{
    if(!_completeButton)
    {
        _completeButton = [[UIButton alloc] init];
        _completeButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [_completeButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff alpha:0.7] forState:UIControlStateNormal];
        _completeButton.backgroundColor = [UIColor yc_colorWithUInt:0x000000 alpha:0.6];
        _completeButton.layer.cornerRadius = 17.5;
        [_completeButton setTitle:NSLocalizedString(@"完成", nil) forState:UIControlStateNormal];
        [_completeButton addTarget:self action:@selector(didTouchUpInsideCompleteButton:) forControlEvents:UIControlEventTouchUpInside];
        _completeButton.hidden = YES;
        
    }
    return _completeButton;
}

#pragma mark - 生命周期

- (instancetype)initWithUserModel:(YCLoginUser *)userModel
{
    if (self = [super init]) {
        _userModel = userModel;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    self.view.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    [self makeView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)makeView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.alwaysBounceVertical = NO;
    
    [self.view addSubview:self.scrollView];
    self.scrollView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    UIImageView *contentImageView = [[UIImageView alloc] init];
    contentImageView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
//    contentImageView.image = [UIImage imageNamed:@"mf_bg_img"];
    contentImageView.userInteractionEnabled = YES;
    
    // 添加一个黑色背景
    UIView *viewBg = [[UIView alloc] initWithFrame:contentImageView.bounds];
    viewBg.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [contentImageView addSubview:viewBg];
    [viewBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(contentImageView);
    }];
    
    //毛玻璃效果
    FXBlurView * aview = [[FXBlurView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    aview.dynamic = YES;
    aview.tintColor = [UIColor clearColor];  //前景颜色
    aview.blurRadius = 10;                 //模糊半径，View的模糊效果程度
    [contentImageView addSubview:aview];
    [aview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(contentImageView);
    }];
    self.contentImageView = contentImageView;
    [self.scrollView addSubview:contentImageView];
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.contentImageView addSubview:self.improveInfoLabel];
    [self.contentImageView addSubview:self.nickerAstroLabel];
    [self.contentImageView addSubview:self.placeLabel];
    [self.contentImageView addSubview:self.iconBgImageView];
    [self.contentImageView addSubview:self.birthdyButton];
    [self.contentImageView addSubview:self.placeButton];
    [self.contentImageView addSubview:self.completeButton];
    
    [self.iconBgImageView addSubview:self.iconImageView];
    
    [_improveInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentImageView).offset(heightFromIphone6Height(60));
        make.centerX.equalTo(_contentImageView);
        make.height.equalTo(@32);
    }];
    
    [_iconBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_improveInfoLabel.mas_bottom).offset(heightFromIphone6Height(55));
        make.centerX.equalTo(self.contentImageView);
        [YCAdapter performOtherBlock:^{
            make.width.height.equalTo(@190);
        } IPhone6Block:^{
            make.width.height.equalTo(@232);
        } IPhone6pBlock:^{
            make.width.height.equalTo(@240);
        }];
    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        [YCAdapter performOtherBlock:^{
            make.width.height.equalTo(@165);
        } IPhone6Block:^{
            make.width.height.equalTo(@195);
        } IPhone6pBlock:^{
            make.width.height.equalTo(@205);
        }];
        make.center.equalTo(_iconBgImageView);
    }];
    
    [_nickerAstroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconBgImageView.mas_bottom).offset(heightFromIphone6Height(17));
        make.centerX.equalTo(_iconBgImageView);
        make.height.equalTo(@30);
    }];
    
    [_placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nickerAstroLabel.mas_bottom).offset(heightFromIphone6Height(10));
        make.centerX.equalTo(_iconBgImageView);
        make.height.equalTo(@26);
    }];
    
    [_birthdyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_placeLabel.mas_bottom);
        make.centerX.equalTo(self.contentImageView);
        make.left.equalTo(self.contentImageView).offset(47);
        make.right.equalTo(self.contentImageView).offset(-47);
        make.height.equalTo(@36);
    }];
    
    [_placeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_birthdyButton.mas_bottom).with.offset(heightFromIphone6Height(14));
        make.centerX.equalTo(self.contentImageView);
        make.left.equalTo(self.contentImageView).offset(47);
        make.right.equalTo(self.contentImageView).offset(-47);
        make.height.equalTo(@36);
    }];
    
    [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_placeButton.mas_bottom).with.offset(heightFromIphone6Height(68));
        make.centerX.equalTo(self.contentImageView);
        make.left.equalTo(self.contentImageView).offset(47);
        make.right.equalTo(self.contentImageView).offset(-47);
        make.height.equalTo(@36);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_completeButton.mas_bottom).offset(heightFromIphone6Height(66));
    }];
    
    [self.view layoutIfNeeded];

    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.contentImageView.frame)-64);
}


#pragma mark -- 响应事件

///生日年月
- (void)didTouchUpInsideBirthdayButton:(UIButton *)button
{
    YCLog(@"生日年月");
    NSDate *date = [@"1996-08-15" yc_dateWithDateFormatString:@"yyyy-MM-dd"];
    if(date)
    {
        self.datePicker.date = date;
    }
    
    self.pickerContainerView.title = NSLocalizedString(@"选择出生日期", nil);
    [self.pickerContainerView showDatePicker:self.datePicker inView:self.navigationController.view];

}
///居住于
- (void)didTouchUpInsidePlaceButton:(UIButton *)button
{
//    YCLog(@"居住于");
//    
//    NSString *province = @"广东";
//    NSString *city = @"广州市";
//    
//    if([city hasSuffix:@"市"])
//    {
//        city = [city substringToIndex:city.length - 1];
//    }
//    
//    NSUInteger pIndex = [self.citys indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
//        return [obj[@"name"] isEqualToString:province];
//    }];
//    
//    if(pIndex < self.citys.count)
//    {
//        NSArray *citys = self.citys[pIndex][@"city"];
//        
//        NSUInteger cIndex = [citys indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
//            return [obj[@"name"] isEqualToString:city];
//        }];
//        
//        if(cIndex < citys.count)
//        {
//            [self.addressPickerView selectRow:pIndex inComponent:0 animated:NO];
//            [self.addressPickerView selectRow:cIndex inComponent:1 animated:NO];
//        }
//    }
//    
//    
//    self.pickerContainerView.title = NSLocalizedString(@"选择居住地", nil);
//    
//    [self.pickerContainerView showPickerView:self.addressPickerView inView:self.navigationController.view];
    
    NSString *province = @"广东";
    NSString *city = @"广州市";
    NSString *district = @"天河区";
    NSUInteger pIndex = [self.citys indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj[@"name"] isEqualToString:province];
    }];
    
    if(pIndex < self.citys.count)
    {
        NSArray *citys = self.citys[pIndex][@"citys"];
        
        NSUInteger cIndex = [citys indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            return [obj[@"name"] isEqualToString:city];
        }];
        
        NSArray *areas = citys[cIndex][@"ares"];
        
        NSUInteger aIndex = [areas indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            return [obj isEqualToString:district];
        }];
        
        if(cIndex < citys.count)
        {
            [self.addressPickerView selectRow:pIndex inComponent:0 animated:NO];
            [self.addressPickerView selectRow:cIndex inComponent:1 animated:NO];
        }
        
        if (aIndex < [areas count])
        {
            [self performSelector:@selector(relayTime:) withObject:[NSNumber numberWithInteger:aIndex] afterDelay:0.1];
        }
        
    }
    self.pickerContainerView.title = NSLocalizedString(@"地区", nil);
    [self.pickerContainerView showPickerView:self.addressPickerView inView:self.navigationController.view];
    
    [self.addressPickerView reloadAllComponents];
    
}
-(void)relayTime:(NSNumber*)_aIndex
{
    [self.addressPickerView selectRow:[_aIndex integerValue] inComponent:2 animated:NO];
}

// 完成注册
- (void)didTouchUpInsideCompleteButton:(UIButton *)btn
{
    
    if (!self.userModel.userAvatar.length || !self.userModel.userBgimg) {
        [YCAlertView showMsg:@"请设置头像"];
        return;
    };
    
    if (!self.userModel.userBirthday.length) {
        [YCAlertView showMsg:@"请选择出生年月"];
        return;
    }
    
    if (self.userModel.userProvince.length == 0 ||
        self.userModel.userCity.length == 0 ||
        self.userModel.userDistrict.length == 0)
    {
        [YCAlertView showMsg:@"请选择出生地"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"注册中..."];
    
    /********* 仅仅是为了测试归档功能begin ***********/
    // 1.不加密归档 测试可行
//    [NSKeyedArchiver archiveRootObject:self.userModel toFile:@"/Users/gmall/Desktop/userInfo.data"];
//    // 解档
//    YCLoginUser *newLoginUser = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/gmall/Desktop/userInfo.data"];
    
    // 2.归档并加密处理 测试可行
//    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.userModel];
//    NSString *filePath = @"/Users/gmall/Desktop/userEncryptInfo.data";
//    data = [data yc_encryptWithKey:@"ycschool" iv:@"ycschool"];
//    [data writeToFile:filePath atomically:YES];
    
    // 解密处理
//    NSData *data1 = [NSData dataWithContentsOfFile:filePath];
//    data1 = [data1 yc_decryptWithKey:@"ycschool"  iv:@"ycschool" ];
//    YCLoginUser *model = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
    /********** 仅仅是为了测试归档功能end ************/
    NSDictionary *params = @{
                             @"uid":@(self.userModel.userId),
                             @"token":self.userModel.userToken,
                             @"avatar":self.userModel.userAvatar,
                             @"birthday":self.userModel.userBirthday,
                             @"bgimg":self.userModel.userBgimg,
                             @"province":self.userModel.userProvince,// 接口是整型
                             @"city":self.userModel.userCity,//接口是整型
                             @"district":self.userModel.userDistrict,//接口是整型
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIRegisterImprove params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {

        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"注册成功"];
        [YCLoginUser loginWithUser:self.userModel];// 保存信息
        
        //把用户名保存到偏好设置
        [[NSUserDefaults standardUserDefaults] setObject:self.userModel.userNickname forKey:@"userNicknameKey"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        // 回到登录页
        [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [SVProgressHUD dismiss];
    }];
    
}

// 点击头像-设置头像
- (void)tapIcon:(UITapGestureRecognizer *)tap
{
    YCLogFunc
    YCRegisterImprovePhotoPickerView *photoPickerView = [[YCRegisterImprovePhotoPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:photoPickerView];
    [photoPickerView showWithTakePhotoClickBlock:^(UIImage *image) {
        UIImage *takeimage = image;
        YCLog(@"进入拍照回调");
        //设置图片
        self.contentImageView.image = image;
        self.iconImageView.image = image;
        
        // 图片加密
        self.userModel.userAvatar = [NSString  yc_stringWithBase64FromUIImage:image];
        self.userModel.userBgimg = self.userModel.userAvatar;
        
        self.improveInfoLabel.hidden = YES;//隐藏 完善个人资料
        // 改变头像离顶部约束
        [self.iconBgImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentImageView).offset(heightFromIphone6Height(114));
        }];
        // 改变出生于,现居于的背景
        self.birthdyButton.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
        self.placeButton.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
        [self.birthdyButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.6] forState:UIControlStateNormal];
        [self.placeButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.6] forState:UIControlStateNormal];
        
        // 都设置了内容,显示完成按钮
        if (_placeLabel.text.length!=0 && _nickerAstroLabel.text!=0 && _contentImageView.image!= nil) {
            _completeButton.hidden = NO;
        }
        
    } selectPhotoClickBlock:^(UIImage *image) {
        UIImage *selectimage = image;
        YCLog(@"进入选择相片回调");
        //设置图片
        self.contentImageView.image = image;
        self.iconImageView.image = image;
        
        // 图片加密
        self.userModel.userAvatar = [NSString  yc_stringWithBase64FromUIImage:image];
        self.userModel.userBgimg = self.userModel.userAvatar;
        
        self.improveInfoLabel.hidden = YES;//隐藏 完善个人资料
        // 改变头像离顶部约束
        [self.iconBgImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentImageView).offset(heightFromIphone6Height(114));
        }];
        // 改变出生于,现居于的背景
        self.birthdyButton.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
        self.placeButton.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
        [self.birthdyButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.6] forState:UIControlStateNormal];
        [self.placeButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.6] forState:UIControlStateNormal];
        
        // 都设置了内容,显示完成按钮
        if (_placeLabel.text.length!=0 && _nickerAstroLabel.text!=0 && _contentImageView.image!= nil) {
            _completeButton.hidden = NO;
        }
    } cancelBlock:^{
        YCLog(@"取消回调");
    }];
}

#pragma mark - UIPickerView delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        NSUInteger rows = self.citys.count;
        
        return rows;
    }
    else if (component == 1)
    {
        NSDictionary *pro = self.citys[[pickerView selectedRowInComponent:0]];
        NSUInteger rows = [pro[@"citys"] count];
        
        return rows;
    }
    else
    {
        NSInteger selectedRow = [pickerView selectedRowInComponent:0];
        NSArray *citys = self.citys[selectedRow][@"citys"];
        selectedRow = [pickerView selectedRowInComponent:1];
        NSArray *ares = citys[selectedRow][@"ares"];
        NSUInteger rows = [ares count];
        return rows;
    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title;

    if(component == 0)
    {
        title = self.citys[row][@"name"];
    }
    else if(component == 1)
    {
        NSArray *citys = self.citys[[pickerView selectedRowInComponent:0]][@"citys"];
        if(row < citys.count)
        {
            title = citys[row][@"name"];
        }
    }
    else
    {
        NSArray *areas = self.citys[[pickerView selectedRowInComponent:0]][@"citys"][[pickerView selectedRowInComponent:1]][@"ares"];
        if(row < areas.count)
        {
            title = areas[row];
        }
    }
    
    return title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        [pickerView reloadAllComponents];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
    else if (component == 1)
    {
        [pickerView reloadAllComponents];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
    else
    {
        [pickerView reloadAllComponents];
    }
}

#pragma mark - YCPickerContainerViewDelegate
// 选择居住于点击确定时
- (void)pickerContainerViewDidDone:(YCPickerContainerView *)containerView
{
    if (containerView.pickerView == self.datePicker)
    {
        NSDate *date = self.datePicker.date;
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
        NSInteger year = nowCmps.year;
        NSInteger month = nowCmps.month;
        NSInteger day = nowCmps.day;
        NSString *astroString = [NSString yc_getAstroWithMonth:month day:day];
        self.nickerAstroLabel.text = [NSString stringWithFormat:@"%@ %@",self.userModel.userNickname,astroString];
        self.nickerAstroLabel.hidden = NO;
        self.birthdyButton.hidden = YES;
        
        self.userModel.userHoroscope = astroString;//星座
        self.userModel.userBirthday = [NSString stringWithFormat:@"%zd-%02zd-%02zd 00:00:00",year,month,day];
    
    } else {
        NSUInteger pIndex = [self.addressPickerView selectedRowInComponent:0];
        NSUInteger cIndex = [self.addressPickerView selectedRowInComponent:1];
        NSUInteger aIndex = [self.addressPickerView selectedRowInComponent:2];

        NSDictionary *provinceDict = self.citys[pIndex];
        NSString *pro = provinceDict[@"name"];
        NSArray *citys = self.citys[pIndex][@"citys"];
        NSString *city = citys[cIndex][@"name"];
        NSArray *areas = self.citys[pIndex][@"citys"][cIndex][@"ares"];
        NSString *district = areas[aIndex];
        
        NSString *placeString = nil;
        placeString = [NSString stringWithFormat:@"%@ %@ %@",pro,city,district];
        self.placeLabel.text = placeString;
        self.placeLabel.hidden = NO;//显示出来
        self.placeButton.hidden = YES;//隐藏按钮
        
        self.userModel.userProvince = pro;
        self.userModel.userCity = city;
        self.userModel.userDistrict = district;
    }
    
    // 都设置了内容,显示完成按钮
    if (_placeLabel.text.length!=0 && _nickerAstroLabel.text!=0 && _contentImageView.image!= nil) {
        _completeButton.hidden = NO;
    }
}


@end
